<?php
/**
*
* @package RCB - R/C Booking
* @version $Id: view.php 8 2009-02-12 14:39:45Z scott $
* @copyright (c) 2008 Scott Deakin
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'rcb/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

page_header('RC Booking');

$template->set_filenames(array(
    'body' => 'rcb/view_body.html',
));

$meeting_id = request_var('meeting_id',0);

/********************************************************/

$sql = 'SELECT date, description, status
		FROM ' . RCB_MEETING_TABLE . '
		WHERE meeting_id = ' . (int) $meeting_id;
$result = $db->sql_query($sql);

if($row = $db->sql_fetchrow($result))
{
	$template->assign_vars( array(
		'DESCRIPTION' => $row['description'],
		'DATE' => $row['date'],
		'STATUS' => $row['status']
	));
}

/********************************************************/
$sql = 'SELECT c.class_id, c.description, c.max_size
		FROM ' . RCB_CLASS_TABLE .' as C
		INNER JOIN ' . RCB_MEETING_CLASS_TABLE . ' as mc
		ON mc.class_id = c.class_id
			AND mc.meeting_id = ' . (int) $meeting_id;
$result = $db->sql_query($sql);
$classes = $db->sql_fetchrowset($result);

// now loop around each class 

foreach($classes as $class)
{

	$template->assign_block_vars('class', array(
		'CLASS_ID' => $class['class_id'],
		'DESCRIPTION' => $class['description']
	));


	$sql = 'SELECT d.name, d.brca
			FROM ' . RCB_DRIVER_TABLE . ' AS d
			INNER JOIN ' . RCB_BOOKING . ' AS b
				ON b.user_id = d.user_id
				AND b.meeting_id = ' . (int) $meeting_id .'
				AND b.class_id = ' . (int) $class['class_id'];
	$result = $db->sql_query($sql);
	$drivers = $db->sql_fetchrowset($result);

	$max_heat_size = $class['max_size'];
 	$remaining = sizeof($drivers);	
	$driver_index = 0;
	$heat = 1;

	while($remaining > 0)
	{

		$heats = ceil($remaining / $max_heat_size);
		$num_drivers = ceil($remaining / $heats);

		$template->assign_block_vars('class.heat', array(
			'HEAT' => $heat,
			'DRIVERS' => $num_drivers
		));	
		for($i = $driver_index; $i < ($num_drivers + $driver_index); $i++)
		{
			$template->assign_block_vars('class.heat.driver', array(
				'NAME' => $drivers[$i]['name'],
				'BRCA' => $drivers[$i]['brca']
			));
		}
		$heat++;
		$driver_index += $num_drivers;
		$remaining -= $num_drivers;
	}
	
}
		
/********************************************************/

page_footer();
?>

